<template>
  <div class="home_container">
    <div class="home_header main-box">
      <div class="home_header_box">
        <router-link to="/home">
          <div class="header_logo_box">
            <img class="header_logo" src="../assets/logo.png" alt="主页"/>
          </div>
        </router-link>
        <div class="header_user_box">
          <div class="header_user" v-if="user.id">
            <a-tooltip placement="bottom" title="消息中心">
              <router-link class="header_user_item" to="/message">
                <BellOutlined/>
              </router-link>
            </a-tooltip>
            <a-tooltip placement="bottom" title="写博客">
              <router-link to="/blog/edit">
                <div class="header_user_item">
                  <PlusOutlined/>
                </div>
              </router-link>
            </a-tooltip>
            <a-dropdown placement="bottomCenter">
              <div class="header_user_item">
                <a-avatar :size="30" :src="user.avatar"/>
              </div>
              <template #overlay>
                <a-menu>
                  <a-menu-item>
                    <router-link :to="'/u/'+user.domain">我的主页</router-link>
                  </a-menu-item>
                  <a-menu-item>
                    <router-link to="/settings">个人中心</router-link>
                  </a-menu-item>
                  <a-menu-item v-if="hasManage">
                    <router-link to="/manage">管理后台</router-link>
                  </a-menu-item>
                  <a-menu-divider/>
                  <a-menu-item @click="logout">
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div @click="toLogin" v-else>
            <a-button type="primary">登录</a-button>
          </div>
        </div>
      </div>
    </div>
    <div class="home_content main-box">
      <div class="home_content_box">
        <router-view/>
      </div>
    </div>
  </div>
</template>

<script>
import { computed, defineComponent, reactive, toRefs } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { BellOutlined, PlusOutlined } from '@ant-design/icons-vue'
import { Logout } from '@/service/user'

export default defineComponent({
  setup () {
    const route = useRoute()
    const router = useRouter()
    const store = useStore()
    const state = reactive({
      user: computed(() => store.getters.user),
      hasManage: computed(() => store.getters.manageRouteList.length > 0)
    })

    const methods = reactive({
      toLogin: () => {
        router.push({
          name: 'login',
          params: {
            path: route.path
          }
        })
      },
      logout: () => {
        store.dispatch('logout')
        Logout()
        router.push('/login')
      }
    })

    return {
      ...toRefs(state),
      ...toRefs(methods)
    }
  },
  components: {
    BellOutlined,
    PlusOutlined
  }
})
</script>

<style lang="less">
@header-height: 55px;

.home_container {
  width: 100%;

  & .home_header {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
    width: 100%;
    height: @header-height;
    background-color: #333;
    color: #fff;
    border-bottom: 1px solid #00000020;
    display: flex;
    align-items: center;
    justify-content: center;

    & .home_header_box {
      width: 80%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;

      & a {
        color: #fff;
      }

      & .header_logo_box {
        width: 170px;
        overflow-x: hidden;

        &:hover {
          background-color: #ffffff20;
        }

        & .header_logo {
          height: @header-height;
        }
      }

      & .header_user_box {
        width: 10%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: flex-end;

        & .header_user {
          width: 100%;
          height: 100%;
          font-size: 20px;
          display: flex;
          align-items: center;
          justify-content: space-around;

          & .header_user_item {
            width: 40px;
            height: @header-height;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;

            &:hover {
              background-color: #ffffff20;
            }

            & a {
              display: flex;
              align-items: center;
            }
          }
        }
      }
    }
  }

  & .home_content {
    position: absolute;
    top: @header-height;
    width: 100%;
    min-height: calc(100% - @header-height);
    background-color: #00000010;
    display: flex;
    justify-content: center;

    & .home_content_box {
      width: 80%;
    }
  }
}

html, body, #app {
  height: 100%;
  margin: 0;
  padding: 0;
}

textarea {
  resize: none;
}

.home_child_container {
  width: 100%;
  padding: 10px 0;
  display: flex;
  justify-content: space-between;
}

.main-box {
  min-width: 1600px;
}

@media screen and (max-width: 1400px) {
  .home_container {
    & .home_header {
      & .home_header_box {
        width: 95%;

        & .header_user_box {
          width: 20%;
        }
      }
    }

    & .home_content {
      & .home_content_box {
        width: 100%;
      }
    }
  }

  .home_child_container {
    padding: 5px 0;
  }

  .main-box {
    min-width: 0;
  }
}

@media screen and (max-width: 1000px) {
  .home_container {
    & .home_header {
      & .home_header_box {
        & .header_user_box {
          width: 30%;
        }
      }
    }
  }
}
</style>
